﻿using System;

namespace DataScripter.Database.SqlServer
{
    static class SqlServerSyntax
    {
        private const string NULL = "NULL";

        public static string ToSql(DateTime? value)
        {
            return value.HasValue
                ? string.Format("'{0}-{1:00}-{2:00}'", value.Value.Year, value.Value.Month, value.Value.Day)
                : NULL;
        }

        public static string ToSql(DateTime? value, bool isDate)
        {
            return value.HasValue
                ? isDate 
                    ? string.Format("'{0}-{1:00}-{2:00}'", value.Value.Year, value.Value.Month, value.Value.Day)
                    : string.Format("'{0}-{1:00}-{2:00} {3:00}:{4:00}:{5:00}.{6:000}'", value.Value.Year, value.Value.Month, value.Value.Day, value.Value.Hour, value.Value.Minute, value.Value.Second, value.Value.Millisecond)
                : NULL;
        }

        public static string ToSql(decimal? value)
        {
            return value.HasValue
                ? string.Format("{0:#.0000}", value.Value).Replace(",", ".")
                : NULL;
        }

        public static string ToSql(int? value)
        {
            return value.HasValue
                ? string.Format("{0}", value.Value)
                : NULL;
        }

        public static string ToSql(bool? value)
        {
            return value.HasValue
                ? value.Value ? "1" : "0"
                : NULL;
        }

        public static string ToSql(Guid? value)
        {
            return value.HasValue 
                ? string.Format("'{0}'", value) 
                : "NULL";
        }

        public static string ToSql(string value)
        {
            if (value == null)
                return NULL;
            return string.Format("'{0}'", value.Replace("'", "''"));
        }

        public static string ToSql(float? value)
        {
            return value.HasValue
                ? string.Format("{0:#.0000}", value.Value).Replace(",", ".")
                : NULL;
        }

        public static string ToSql(double? value)
        {
            return value.HasValue
                ? string.Format("{0:#.0000}", value.Value).Replace(",", ".")
                : NULL;
        }

        //public static string ToSqlCast(byte? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS BYTE)";
        //}

        //public static string ToSqlCast(Int16? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS SMALLINT)";
        //}

        //public static string ToSqlCast(Int32? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS INT)";
        //}

        //public static string ToSqlCast(DateTime? value, bool isDate)
        //{
        //    return string.Format("CAST({0} AS {1})", ToSql(value), (isDate ? "DATE" : "DATETIME"));
        //}

        //public static string ToSqlCast(double? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS FLOAT)";
        //}

        //public static string ToSqlCast(string value)
        //{
        //    return ToSql(value);
        //}

        //public static string ToSqlCast(Guid? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS UNIQUEIDENTIFIER)";
        //}

        //public static string ToSqlCast(bool? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS BIT)";
        //}

        //public static string ToSqlCast(decimal? value)
        //{
        //    return "CAST(" + ToSql(value) + " AS DECIMAL)";
        //}
    }
}
